package net.ghy.statistics.department;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import dao.hb.BxMidle;
import dao.hb.UUser;
import dao.hb.UserBat;

import net.bat.db.BaseDAO;
import net.bat.db.BaseDAOImpl;
import net.ghy.BXService;
import net.ghy.statistics.utils.EUtils;


public class DepExpData {
   //
	double initdata = 0.0;
	static private BaseDAO dao;
	public BaseDAO getDao() {
		return dao;
	}
	static String date= BXService.getCurrYearFirst();

	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}
	
	//将部门与该部门用户信息存入hashmap 退休人员作为退休部门处理
	public Map<String,List> userbxData(List dep,String start,String end) throws Exception{
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		Map dpt_user_bx = new HashMap<String,List>();
		List uid_grp_dpt = null;
        //退休
		String txhql = "select distinct b.uid from UUser u , BxMidle b where u.id = b.uid and b.wkState='退休' and (b.bxtype='门诊 ' or b.bxtype='住院') and ( b.dtLook >= '" +start + "' and b.dtLook <='" + end +"')";
		List tx_users = dao.find(txhql);
		Map<String, List> udtx = getUserDetailGroupByDpt(tx_users,start, end,"退休"); 
		dpt_user_bx.put("退休", udtx);
		//在职
		String zz = "select distinct b.uid from UUser u , BxMidle b where u.id = b.uid and (b.bxtype='门诊 ' or b.bxtype='住院') and b.wkState='在职' and u.dpt = '";
		if(dep != null && dep.size() > 0){
			for(int i = 0 ; i < dep.size() ; i++){
				String hql =  zz + dep.get(i)+"'" + "  and (b.bxtype='门诊 ' or b.bxtype='住院') and ( b.dtLook >= '" +start + "' and b.dtLook <='" + end +"')";
				uid_grp_dpt = dao.find(hql);
				Map<String, List> udgbd = getUserDetailGroupByDpt(uid_grp_dpt,start, end,"在职"); 
				dpt_user_bx.put(dep.get(i), udgbd );
			}
		}
		return dpt_user_bx;
	}
	
	
	//按照部门获取人员
	public Map<String, List> getUserDetailGroupByDpt(List uidgrpdpt,String begin,String end,String wkstate) throws Exception{
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		Map<String, List> userdptbxlist = new HashMap<String, List>();
		//String start = "select u.name,b.* from UUser u, BxMidle b where u.id = b.uid and b.uid = ";
		String start = "from BxMidle where uid = ";
		Map<String, Object> tjdptuser = new HashMap<String, Object>();
		List<Map> dpt_bxdetail = new ArrayList<Map>();
		BxMidle mtj = new BxMidle();
		BxMidle ztj = new BxMidle();
		BxMidle mzdptu = new BxMidle(0.0,0.0,0.0,0.0);
		BxMidle zydptu = new BxMidle(0.0,0.0,0.0,0.0);
		BxMidle baogf = new BxMidle(0.0,0.0,0.0,0.0);
		if(uidgrpdpt != null && uidgrpdpt.size() > 0){
			for(int i = 0 ; i < uidgrpdpt.size() ; i++){
				List<Map> uid_bxdetail = new ArrayList<Map>();
				String hql = start + uidgrpdpt.get(i) + " and wkState='"+wkstate+"' and ( dtLook >= '" +begin + "' and dtLook <='" + end +"')";//adddd
				Map<String, BxMidle> mzxuser = getTJuser(hql, "门诊");//单用户门诊统计
				Map<String, BxMidle> zybxuser = getTJuser(hql, "住院"); //单用户住院统计
				//单用户扣除包干费
				if(begin.contains("01-01")){
					if(wkstate.equals("在职")){
					Map<String, BxMidle> bgfuser = subbagf(mzxuser,zybxuser,begin,end, uidgrpdpt.get(i).toString());
					uid_bxdetail.add(bgfuser);
					baogf = dptsubbagf(bgfuser,baogf);
				  }else {
					  Map<String, BxMidle> bgfuser = subbagftx(mzxuser,zybxuser,begin,end, uidgrpdpt.get(i).toString()); 
					  uid_bxdetail.add(bgfuser);
					  baogf = dptsubbagf(bgfuser,baogf);
				  }
				}
				//整个部门扣除包干费
				mtj = getMzTjdpt(mzxuser,"门诊",mzdptu);    //整个部门门诊统计
				ztj = getZyTjdpt(zybxuser,"住院",zydptu);   //整个部门住院统计
				uid_bxdetail.add(mzxuser);
				uid_bxdetail.add(zybxuser);
				String uname = EUtils.getUserbyid(uidgrpdpt.get(i).toString()).getName();
				userdptbxlist.put(uname,uid_bxdetail);//add dpt 统计信息
			}
			Map<String, BxMidle> dptmztj =new HashMap<String, BxMidle>();
			dptmztj.put("门诊", mtj);
			dptmztj.put("住院", ztj);
			if(begin.contains("01-01"))
				dptmztj.put("包干费", baogf);
			dpt_bxdetail.add(dptmztj);
			userdptbxlist.put("合计",dpt_bxdetail);
		
		}
		return userdptbxlist;
	}
	//获取该部门各个用户的报销金额 type:mz,zy
	public  Map<String, BxMidle> getTJuser(String hql,String type){
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		Map<String, BxMidle>  detail =new HashMap<String, BxMidle>();
		List bxdetail = new ArrayList();
		double cpayone = 0.0,cpaytwo = 0.0,cpayall = 0.0,ctotal=0.0;
		String mz_hql = hql + " and bxtype='"+type + "'";
		bxdetail = dao.find(mz_hql);
		BxMidle stabyuid = new BxMidle();
		if(bxdetail!=null && bxdetail.size() > 0){
			for(int i = 0 ; i < bxdetail.size(); i++ ){
				BxMidle bm = (BxMidle) bxdetail.get(i);
				cpayone = cpayone + Math.round(bm.getPayone()*100);
				cpaytwo = cpaytwo + Math.round(bm.getPaytwo()*100);
				cpayall = cpayall + Math.round(bm.getPayall()*100);	
				ctotal = ctotal + Math.round(bm.getBxSum()*100);
			}
		}
		stabyuid.setPayone(cpayone/100);
		stabyuid.setPaytwo(cpaytwo/100);
		stabyuid.setPayall(cpayall/100);
		stabyuid.setBxSum(ctotal/100);
		detail.put(type, stabyuid);
		//do something 
		return detail;
		
	}  
	
	public BxMidle getMzTjdpt(Map<String, BxMidle> dptuser,String key,BxMidle mzdptu){
		BxMidle single = (BxMidle) dptuser.get(key);
		double cpayone = Math.round(mzdptu.getPayone()*100),cpaytwo = Math.round(mzdptu.getPaytwo()*100),cpayall =Math.round( mzdptu.getPayall()*100),ctotal=Math.round(mzdptu.getBxSum()*100);
		cpayone +=Math.round(single.getPayone()*100);
		cpaytwo +=Math.round(single.getPaytwo()*100);
		cpayall +=Math.round(single.getPayall()*100);
		ctotal +=Math.round(single.getBxSum()*100);
		mzdptu.setPayone(cpayone/100);
		mzdptu.setPaytwo(cpaytwo/100);
		mzdptu.setPayall(cpayall/100);
		mzdptu.setBxSum(ctotal/100);
		return mzdptu;
	}
	
	public BxMidle getZyTjdpt(Map<String, BxMidle> dptuser,String key,BxMidle zydptu){
		BxMidle single = (BxMidle) dptuser.get(key);
		double cpayone = Math.round(zydptu.getPayone()*100),cpaytwo = Math.round(zydptu.getPaytwo()*100),cpayall = Math.round(zydptu.getPayall()*100),ctotal=Math.round(zydptu.getBxSum()*100);
		cpayone +=Math.round(single.getPayone()*100);
		cpaytwo +=Math.round(single.getPaytwo()*100);
		cpayall +=Math.round(single.getPayall()*100);
		ctotal +=Math.round(single.getBxSum()*100);
		zydptu.setPayone(cpayone/100);
		zydptu.setPaytwo(cpaytwo/100);
		zydptu.setPayall(cpayall/100);
		zydptu.setBxSum(ctotal/100);
		return zydptu;
	}
	
	
	//获取用户包干费
	public double getBaogf(String uid,String start,String end) throws Exception {
		Double baogf = 0.0;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date startdate = df.parse(start);
		Date enddate = df.parse(end);
        int startyear = startdate.getYear()+1900;
        int endyear = enddate.getYear()+ 1900;      
        String hql =  " from UUser A  WHERE A.id = " + uid +" and A.subbgfFlag = 'false'  and (A.year >= " +startyear+ " and A.year<= "+endyear+")" ;
    	if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
        List ub = dao.find(hql);
        if(ub!=null && ub.size()>0){
            for(int i=0;i<ub.size();i++){
            	UUser a = (UUser)ub.get(i);
            	baogf+=a.getBaogf();
            }
        }
		return baogf;
	}
	
	//single user sub baogf
	
	public Map<String, BxMidle> subbagf(Map<String, BxMidle> mzxuser,
	Map<String, BxMidle> zybxuser,String begin,String end,String uid) throws Exception{
		BxMidle bgf = new BxMidle();
		double b = getBaogf(uid,begin,end);
		bgf.setPayone(b);
		double  bxmzuser = mzxuser.get("门诊").getBxSum();
		double bxzyuser = zybxuser.get("住院").getBxSum();
		double total = Math.round(bxmzuser*100+bxzyuser*100)/100.0;
		bgf.setPayall(total);
		bgf.setBxSum(Math.round(total*100-b*100)/100.0);
		Map<String, BxMidle> bgfuser = new HashMap<String, BxMidle>();
		bgfuser.put("包干费",bgf);
	    return bgfuser;
	}
	
	public Map<String, BxMidle> subbagftx(Map<String, BxMidle> mzxuser,
	Map<String, BxMidle> zybxuser,String begin,String end,String uid) throws Exception{
		BxMidle bgf = new BxMidle();
		double b = 0.0;
		bgf.setPayone(b);
		double  bxmzuser = mzxuser.get("门诊").getBxSum();
		double bxzyuser = zybxuser.get("住院").getBxSum();
		double total = Math.round(bxmzuser*100+bxzyuser*100)/100.0;
		bgf.setPayall(total);
		bgf.setBxSum(Math.round(total*100-b*100)/100.0);
		Map<String, BxMidle> bgfuser = new HashMap<String, BxMidle>();
		bgfuser.put("包干费",bgf);
	    return bgfuser;
	}
	
	//dtp sub gaogf
	public BxMidle dptsubbagf(Map<String, BxMidle> bgfuser,BxMidle baogf){
		BxMidle single = (BxMidle) bgfuser.get("包干费");
		double cpayone = Math.round(baogf.getPayone()*100),cpayall =Math.round( baogf.getPayall()*100),ctotal=Math.round(baogf.getBxSum()*100);
		cpayone +=Math.round(single.getPayone()*100);
		cpayall +=Math.round(single.getPayall()*100);
		ctotal +=Math.round(single.getBxSum()*100);
		baogf.setPayone(cpayone/100);
		baogf.setPayall(cpayall/100);
		baogf.setBxSum(ctotal/100);
	    return baogf;
	}
}